﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using WebEntities;
using WebEntities.Models.Framework;
using com.subb.WebFramework;

namespace WebUI.Areas.WebAdmin.Controllers
{
    [Authorize]
    public class MemDefModualController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetAllModualByPage(StoreRequestParameters parms)
        {
            int ttlCount;
            FW_MemDefModualDAL mdal = new FW_MemDefModualDAL();
            List<MemDefModual> modList = mdal.GetAllDefUIModual(HttpContext.User.Identity.Name, parms.Start, parms.Limit, out ttlCount);
            return this.Store(modList, ttlCount);
        }

        public ActionResult GetAllModual()
        {
            FW_MemDefModualDAL mdal = new FW_MemDefModualDAL();
            List<MemDefModual> modList = mdal.GetAllDefUIModual(HttpContext.User.Identity.Name);
            return this.Store(modList);
        }

        public ActionResult GetAllMenuItems()
        {
            FW_MenuItemDAL mdal = new FW_MenuItemDAL();
            List<FW_MenuItem> dbitemlist = mdal.GetAllMenuItems();
            List<UIMenuItem> itemlist = new List<UIMenuItem>();
            foreach (var item in dbitemlist)
            {
                //这里不需要显示MenuName和ParentItemName，所以赋值为空
                if (!string.IsNullOrWhiteSpace(item.ItemUrl))
                {
                    UIMenuItem muitem = UIMenuItem.AsUIMenuItem(item, "", "");
                    itemlist.Add(muitem);
                }
            }
            return this.Store(itemlist);
        }

        [HttpPost]
        public ActionResult DeleteModual(List<MemDefModual> delMods)
        {
            try
            {
                FW_MemDefModualDAL mdal = new FW_MemDefModualDAL();
                foreach (var mod in delMods)
                {
                    FW_MemDefModual modual = mdal.GetModual(mod.ModID);
                    if (modual != null)
                    {
                        if (modual.MemberID.Equals(HttpContext.User.Identity.Name, StringComparison.CurrentCultureIgnoreCase))
                        {
                            mdal.RemoveModual(modual.ModID);
                        }
                    }
                }
                return this.Direct(new { Success = true, Msg = "删除成功！" });
            }
            catch (Exception ex)
            {
                return this.Direct(new { Success = false, Msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message });
            }
        }

        public ActionResult NewModualWindow()
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "新建模块链接",
                Width = 500,
                Height = 300,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.TextListBullets,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("CreateModual"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Render();
            return this.Direct();
        }

        public ActionResult CreateModual()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateModual(MemDefModual v_modual)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                v_modual.MemberID = HttpContext.User.Identity.Name;
                FW_MemDefModualDAL mmdal = new FW_MemDefModualDAL();
                if (string.IsNullOrWhiteSpace(v_modual.ModualName) && v_modual.MenuItemID.HasValue)
                {
                    FW_MenuItemDAL midal = new FW_MenuItemDAL();
                    FW_MenuItem muItem = midal.GetMenuItem(v_modual.MenuItemID.Value);
                    if (muItem != null)
                    {
                        v_modual.ModualName = muItem.ItemName;
                    }
                }
                mmdal.CreateModual(v_modual);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "模块链接创建成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }

        public ActionResult EditModualWindow(int modId)
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "编辑模块链接",
                Width = 500,
                Height = 300,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.TextListBullets,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("EditModual"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Loader.Params.Add(new Parameter("modId", modId.ToString()));
            wnd.Render();
            return this.Direct();
        }

        public ActionResult EditModual(int modId)
        {
            FW_MemDefModualDAL mdal = new FW_MemDefModualDAL();
            FW_MemDefModual modual = mdal.GetModual(modId);
            return View(modual);
        }

        [HttpPost]
        public ActionResult EditModual(MemDefModual v_modual)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                FW_MemDefModualDAL mdal = new FW_MemDefModualDAL();
                if (string.IsNullOrWhiteSpace(v_modual.ModualName) && v_modual.MenuItemID.HasValue)
                {
                    FW_MenuItemDAL midal = new FW_MenuItemDAL();
                    FW_MenuItem muItem = midal.GetMenuItem(v_modual.MenuItemID.Value);
                    if (muItem != null)
                    {
                        v_modual.ModualName = muItem.ItemName;
                    }
                }
                mdal.UpdateModual(v_modual.ModID, v_modual);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "模块链接更新成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }
    }
}
